package com.cungmua.app.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cungmua.app.R;
import com.cungmua.app.activity.OrderDetailsActivity;
import com.cungmua.app.handler.OrderHistoryHandler;
import com.cungmua.app.model.OrderHistory;
import com.cungmua.app.utils.AppUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kelvin.nguyen on 7/21/14.
 */
public class OrderHistoryAdapter extends BaseAdapter {
    private final Activity context;
    private final LayoutInflater inflater;
    private List<OrderHistory> dataSource;
    private OrderHistoryHandler handler;
    private int status = 0;
    private final String  IK_ORDER_ID="orderId";


    public OrderHistoryAdapter(Activity context, OrderHistoryHandler handler) {
        this.context = context;
        this.handler = handler;
        this.inflater = LayoutInflater.from(context);
        this.dataSource = new ArrayList<OrderHistory>();
    }

    /**
     * How many items are in the data set represented by this Adapter.
     *
     * @return Count of items.
     */
    @Override
    public int getCount() {
        return dataSource.size();
    }

    public List<OrderHistory> getDataSource() {
        return dataSource;
    }

    public void addToDataSource(List<OrderHistory> newPage) {
        this.dataSource.addAll(newPage);
        notifyDataSetChanged();
    }
    public void setDataSource(List<OrderHistory> dataSource) {
        this.dataSource = dataSource;
        notifyDataSetChanged();
    }


    @Override
    public int getItemViewType(int position) {
     //   status = dataSource.get(position).getStatus();
        if (status == 0)
            return 0;
        else
            return 1;
    }



    /**
     * Get the data item associated with the specified position in the data set.
     *
     * @param position Position of the item whose data we want within the adapter's
     *                 data set.
     * @return The data at the specified position.
     */
    @Override
    public OrderHistory getItem(int position) {
        return dataSource.get(position);
    }

    /**
     * Get the row id associated with the specified position in the list.
     *
     * @param position The position of the item within the adapter's data set whose row id we want.
     * @return The id of the item at the specified position.
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        TextView txtMadh, txtNgayDat, txtStatus, txtTienThanhToan;
       // Button btnDestroy;
    }


    /**
     * Get a View that displays the data at the specified position in the data set. You can either
     * create a View manually or inflate it from an XML layout file. When the View is inflated, the
     * parent View (GridView, ListView...) will apply default layout parameters unless you use
     * {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}
     * to specify a root view and to prevent attachment to the root.
     *
     * @param position    The position of the item within the adapter's data set of the item whose view
     *                    we want.
     * @param convertView The old view to reuse, if possible. Note: You should check that this view
     *                    is non-null and of an appropriate type before using. If it is not possible to convert
     *                    this view to display the correct data, this method can create a new view.
     *                    Heterogeneous lists can specify their number of view types, so that this View is
     *                    always of the right type (see {@link #getViewTypeCount()} and
     *                    {@link #getItemViewType(int)}).
     * @param parent      The parent that this view will eventually be attached to
     * @return A View corresponding to the data at the specified position.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final OrderHistory orderHistory=dataSource.get(position);

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.row_order_list, parent, false);
            holder.txtMadh = (TextView) convertView.findViewById(R.id.txtMadh);
            holder.txtNgayDat = (TextView) convertView.findViewById(R.id.txtNgayDat);
            holder.txtStatus = (TextView) convertView.findViewById(R.id.txtStatus);
            holder.txtTienThanhToan = (TextView) convertView.findViewById(R.id.txtTienThanhToan);

            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtMadh.setText(orderHistory.getOrderId()+"");
        holder.txtStatus.setText(orderHistory.getStatus()+"");
        try {
            holder.txtNgayDat.setText(AppUtils.parseServerDate(orderHistory.getTimeCreate().toString())+"");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        holder.txtTienThanhToan.setText(AppUtils.getFullPriceString(orderHistory.getAmountTotal()) + "đ");

        holder.txtMadh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Toast.makeText(context,orderHistory.getOrderId()+"",Toast.LENGTH_LONG).show();
                //IK_ORDER_ID

                Intent intent = new Intent(context, OrderDetailsActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra(IK_ORDER_ID, orderHistory.getOrderId());
                context.startActivity(intent);

            }
        });

        return convertView;
    }
}
